/*
 * @Description:
 * @Author: chenjiaxin
 * @Date: 2022-06-04 16:32:48
 * @LastEditTime: 2022-06-04 17:13:00
 * @FilePath: \leetcode\1_栈结构\01_栈的封装.js
 */
function Stack() {
  // 栈中的属性
  this.items = [];

  // 栈的相关操作
  // 1. 压入栈
  Stack.prototype.push = function (element) {
    this.items.push(element);
  };
  // 2. 出栈
  Stack.prototype.pop = function () {
    return this.items.pop();
  };

  // 3. 查看一下栈顶元素
  Stack.prototype.peek = function () {
    return this.items[this.items.length - 1];
  };

  // 4. 判断栈是否为空
  Stack.prototype.isEmpty = function () {
    return this.items.length == 0;
  };

  // 5. 获取栈中的元素个数
  Stack.prototype.size = function () {
    return this.items.length;
  };

  // 6. toString 方法
  Stack.prototype.toString = function () {
    let resultString = "";
    for (let index = 0; index < this.items.length; index++) {
      resultString += this.items[index] + " ";
    }
    return resultString;
  };
}

// var s = new Stack();
// s.push(10);
// s.push(102);
// s.push(20);
// s.push(40);

// s.pop();
// s.isEmpty();
// s.size();
